package com.platform.os.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

/**
 * @author KangJun 2007-10-07.
 */
public class FileHandle {
	//String fullfileName;	全文件名
	//String fileName;		不带扩展名的文件名
	//String fileExtend;	文件扩展名
	//String path;			系统路径
	//String urlPath;		以项目根目录为起点的路径
	//String filePath;		path + fullfileName
	//String urlFilePath;	urlPath + fullfileName

	/** 文件改名.
	 * @param oldName 旧文件名
	 * @param newName 新文件名
	 * @return boolean
	 */
	public static boolean rename(String oldName, String newName) {
		File sFile = new File(oldName);
		if (!sFile.exists()) {
			return false;
		}
		File dFile = new File(newName);
		if (dFile.exists()) {
			return false;
		}
		if (sFile.isFile()) {
			sFile.renameTo(dFile);
		}
		return true;
	}

	/** 将字符串写入文件.
	 * @param filePath 全路径文件名
	 * @param content 文件内容
	 * @param charset 字符集 utf-8 gb2312 iso8859-1
	 * @return boolean
	 */
	public static boolean writeFile(String filePath, String content, String charset) {
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(filePath);
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charset);
			PrintWriter printWriter = new PrintWriter(outputStreamWriter);
			printWriter.println(content);
			printWriter.close();
			outputStreamWriter.close();
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/** 将字符串以增加方式写入文件.
	 * @param filePath 全路径文件名
	 * @param content 文件内容
	 * @return boolean
	 */
	public static boolean appendFile(String filePath, String content) {
		try {
			RandomAccessFile randomAccessFile = null;
			for (int i = 0; i < 120; i++) {
				try {
					randomAccessFile = new RandomAccessFile(filePath, "rw");
					break;
				} catch (IOException e) {
					Thread.sleep(1000);
				}
			}

			randomAccessFile.seek(randomAccessFile.length());
			byte[] img = content.getBytes("gb2312");
			for (int i = 0; i < img.length; i++) {
				randomAccessFile.write(img[i]);
			}
			randomAccessFile.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 复制文件.
	 * @param sFilePath 源文件
	 * @param dFilePath 目标文件
	 * @return boolean
	 */
	public static boolean copyFile(String sFilePath, String dFilePath) {
		File sFile = new File(sFilePath);
		if (!sFile.exists()) {
			return false;
		}

		try {
			if (sFile.isFile()) {
				File dFile = new File(dFilePath);
				dFile.getParentFile().mkdirs();
				FileInputStream input = new FileInputStream(sFilePath);
				FileOutputStream output = new FileOutputStream(dFilePath);
				byte[] b = new byte[1024 * 5];
				int len;
				while ((len = input.read(b)) != -1) {
					output.write(b, 0, len);
				}
				output.flush();
				output.close();
				input.close();
				return true;
			} else {
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 将一个文件拼接到另一个文件尾.
	 * @param dFilePath 目标文件
	 * @param sFilePath 附加的文件
	 * @return boolean
	 */
	public static boolean catFile(String dFilePath, String sFilePath) {
		try {
			RandomAccessFile randomAccessFile = new RandomAccessFile(dFilePath, "rw");
			randomAccessFile.seek(randomAccessFile.length());
			FileInputStream fileInputStream = new FileInputStream(sFilePath);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
			byte[] buf = new byte[1024];
			int len = 0;
			int totalNum = 0;
			while ((len = bufferedInputStream.read(buf)) != -1) {
				randomAccessFile.write(buf, 0, len);
				totalNum += len;
			}
			bufferedInputStream.close();
			fileInputStream.close();
			randomAccessFile.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/** 删除文件或目录.
	 * @param filepath
	 * @return void
	 */
	public static void deleteFile(String filePath) {
		File file = new File(filePath);
		//如果文件不存在，返回
		if (!file.exists()) {
			return;
		}

		if (!file.isDirectory()) {
			//如果不是目录，直接删除
			file.delete();
		} else {
			//如果是目录
			if (file.listFiles().length == 0) {
				//如果目录为空，删除
				file.delete();
			} else {
				//获得目录内的文件列表
				File delFile[] = file.listFiles();
				int i = file.listFiles().length;
				for (int j = 0; j < i; j++) {
					if (delFile[j].isDirectory()) {
						//如果是子目录，递归调用
						deleteFile(delFile[j].getAbsolutePath());
					}
					delFile[j].delete();
				}
				file.delete();
			}
		}
	}
}
